package br.com.amigooo.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import br.com.amigooo.Session.UserSession;
import br.com.amigooo.model.AccessRequest;
import br.com.amigooo.model.ActiveSession;
import br.com.amigooo.model.Order;
import br.com.amigooo.model.User;
import br.com.amigooo.repository.AccessRequestImpl;
import br.com.amigooo.repository.ActiveSessionImpl;
import br.com.amigooo.repository.OrderImpl;
import br.com.amigooo.repository.UserImpl;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;

@Resource
public class PanelController {

	private Result result;
    private UserSession userSession;
	
    public PanelController(Result result, UserSession userSession) {
        this.result = result;
        this.userSession = userSession;
    }
	
	@Path("/panel")
	public void main() {
		List<AccessRequest> accessList = getUserRequest();
		List<ActiveSession> activeList = getActiveUsers();
		List<Order> orderList = getOrders();
		UserImpl userImpl = new UserImpl();
		
		Set<User> setL = userImpl.getUsersList();
		List<User> uList = new ArrayList<>(setL);
		
		result.include("access", uList);
		result.include("active", uList);
		
//		if(accessList != null && accessList.size() == 0){
//			result.include("access", null);
//		}else{
//			result.include("access", accessList);
//		}
//		
//		if(activeList != null && activeList.size() == 0){
//			result.include("active", null);
//		}else{
//			result.include("active", activeList);
//		}
//		
//		if(orderList != null && orderList.size() == 0){
//			result.include("orders", null);
//		}else{
//			result.include("orders", orderList);
//		}
		
	}

	/**
	 * Busca os pedidos de acesso para a empresa
	 * @return Lista de acessos
	 */
	private List<AccessRequest> getUserRequest(){
		AccessRequestImpl accessRequestImpl = new AccessRequestImpl();
		List<AccessRequest> accessList = null;
		
		try{
			accessList = accessRequestImpl.getAccessByCompany(
				this.userSession.getUser().getEstablishment().getId());
		}catch(RuntimeException e){
			e.printStackTrace();
			
			return null;
		}
		
		return accessList;
	}
	
	/**
	 * Busca os usuarios ativos
	 * @return Lista de usuarios ativos
	 */
	private List<ActiveSession> getActiveUsers(){
		ActiveSessionImpl accessRequestImpl = new ActiveSessionImpl();
		List<ActiveSession> accessRequestList = null;
		
		try{
			accessRequestList = accessRequestImpl.getActiveSessionByCompany(
					this.userSession.getUser().getEstablishment().getId());
		}catch(RuntimeException e){
			e.printStackTrace();
			
			return null;
		}
		
		return accessRequestList;
	}
	
	/**
	 * Busca os pedidos ativos
	 * @return Lista de pedidos
	 */
	private List<Order> getOrders(){
		OrderImpl orderImpl = new OrderImpl();
		List<Order> orderList = null;
		
		try{
			orderList = orderImpl.getOrderByCompany(
					this.userSession.getUser().getEstablishment().getId());
		}catch(RuntimeException e){
			e.printStackTrace();
			
			return null;
		}
		
		return orderList;
	}
}
